﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Text;
using Common; 

public partial class community_topicreplies : MemberPageBase
{
    public string BBSTitle = "", ColumnName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        switch (RequestBase.GetFormString("otype"))
        {
            case "savereply":
                SaveReplay();
                break;
            case "getrepliescount":
                GetRepliesCount();
                break;
            case "loadingreplies":
                RepliesItems();
                break;
        }

        if (!IsPostBack)
        {
            if (Utils.IsNumber(RequestBase.GetQueryString("b_id")))
            {
                Model.BBS.BBSInfo _obj = (new BLL.BBS.BBSBO()).GetBBSInfo(int.Parse(RequestBase.GetQueryString("b_id")));
                BBSTitle = _obj.Title;
                lblTitle.InnerText = _obj.Title;
                lblAuthor.Text = _obj.UserNickName;
                lblPubDate.Text = _obj.LastPostTime.ToString();
                lblContent.Text = _obj.Content;
                txtColumnId.Value = _obj.ColumnID.ToString();
                (new BLL.BBS.BBSBO()).SetClickTotal(RequestBase.GetQueryInt("b_id", 0));

                Model.BBS.BBSColumnInfo _columnobj = (new BLL.BBS.BBSBO()).GetBBSColumn(int.Parse(txtColumnId.Value));
                ColumnName = _columnobj.Name;
            }

            if (string.IsNullOrEmpty(UserName))
            {
                txtLoginState.Value = "0"; 
            }
            else
            {
                txtLoginState.Value = "1";
                txtAuthor.Value = this.UserName;
            }
        }
    }

    #region 保存评论
    Model.BBS.BBSReplyInfo BBSObj
    {
        get
        {
            Model.BBS.BBSReplyInfo _obj = new Model.BBS.BBSReplyInfo();
            _obj.BBSID = int.Parse(RequestBase.GetFormString("b_id"));
            _obj.Content = RequestBase.GetFormString("comments");
            _obj.UserNickName = RequestBase.GetFormString("author");
            _obj.UserID = this.UserId;
            _obj.PublishTime = DateTime.Now;

            return _obj;
        }
    }

    void SaveReplay()
    {
        string result = "";

        if (!string.IsNullOrEmpty(this.UserName))
        {
            bool bl = (new BLL.BBS.BBSBO()).SaveBBSReply(BBSObj);

            if (bl)
                BLL.AjaxHelper.ResponseWrite("succeeded");
            else
                BLL.AjaxHelper.ResponseWrite("error.");
        }
        else
            BLL.AjaxHelper.ResponseWrite("unlogin"); 
    }
    #endregion

    void GetRepliesCount()
    {
        object _obj = BLL.Search.CommonSearch.ExecScalar("BBSReply", "COUNT(1)",
         new string[] { "BBSID" },
         new string[] { "Equal" },
         new string[] { "" + RequestBase.GetFormString("b_id") + "" },
         null,
         null);

        if (_obj != null)
        {
            BLL.AjaxHelper.ResponseWrite(_obj.ToString() + " 条回复");
        }
        else
        {
            BLL.AjaxHelper.ResponseWrite("0 条回复");
        }
    }

    void RepliesItems()
    {
        try
        {
            string ctrl = "~/_controls/ctlComments.ascx";
            Control uc_rep = LoadControl(ctrl);
            Repeater rptRepliesList = (Repeater)uc_rep.FindControl("rptRepliesList");

            DataTable dat = BLL.Search.CommonSearch.ExecSelect("BBSReply",
                     new string[] { "BBSID" },
                     new string[] { "Equal" },
                     new string[] { "" + RequestBase.GetFormString("b_id") + "" },
                     null,
                     null);

            rptRepliesList.DataSource = dat;
            rptRepliesList.DataBind();

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            rptRepliesList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
}